package com.jy.api.dao;

import com.jy.api.domain.ProductMaterial;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface ProductMaterialDao  extends CrudRepository< ProductMaterial, Integer>, JpaSpecificationExecutor<ProductMaterial> {



    List<ProductMaterial> findByUnitId(int unitId);


    ProductMaterial findByNameAndStoreId(String name, String storeId);


    List<ProductMaterial> findByNameLikeAndStoreId(String name, String storeId);


    List<ProductMaterial> findByNameLikeAndStoreIdOrderByUnitIdDesc(String name, String storeId);

    List<ProductMaterial> findByPyLikeAndStoreId(String name, String storeId);


    List<ProductMaterial> findByStoreId(String storeId);


    List<ProductMaterial> findByStoreIdOrderByUnitIdDesc(String storeId);

    Page<ProductMaterial> findByStoreId(String storeId, Pageable pageable);

    Page<ProductMaterial> findListByStoreId(String storeId, Pageable pageable);
    @Transactional
    @Modifying
    @Query(value ="UPDATE `product_material` set `stock_qty` =:quantity WHERE `id`=:id and `store_id`=:storeId  " ,nativeQuery = true)
    void updateQtyById(@Param("quantity") int newSurplus, @Param("id") int skuId, @Param("storeId") String storeId);
}
